﻿Public Class SiteStFoncier
    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Dim idAct As String = ""
    Public databaseName As String
    Private Sub SiteStFoncier_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Entrer = ""
        DataGridViewFoncSite.MultiSelect = False
        DataGridViewFoncSite.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewFoncSite.ReadOnly = True
        majCatg()
        majour()

    End Sub


    Private Sub majCatg()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[TypFoncier]"
            db.Connecter(databaseName)
            Me.ComboBoxCodeCatg.Items.Clear()
            Me.ComboBoxNomCatg.Items.Clear()
            Me.ComboBoxCodeCatg.Items.Add("")
            Me.ComboBoxNomCatg.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxCodeCatg.Items.Add(db.rdr(0))
                Me.ComboBoxNomCatg.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxCodeCatg.SelectedIndex = 0
            Me.ComboBoxNomCatg.SelectedIndex = 0
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomCatg_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomCatg.TextChanged

        If Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomCatg.Text.Length
            Me.ComboBoxNomCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
            Me.ComboBoxNomCatg.SelectionStart = Pos
            Me.ComboBoxNomCatg.SelectionLength = Me.ComboBoxNomCatg.Text.Length - Pos
            Me.ComboBoxCodeCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
        Else
            Me.ComboBoxCodeCatg.Text = ""
        End If
    End Sub

    Private Sub majour()
        Try
            db.myReq = "SELECT * FROM  [dbo].[SITESTFONCIER]"
            db.Connecter(databaseName)
            Me.DataGridViewFoncSite.Rows.Clear()
            While db.rdr.Read()
                DataGridViewFoncSite.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4), db.rdr(5), db.rdr(6))
                idAct = db.rdr(0)
            End While
            db.Disconnecter()

            If DataGridViewFoncSite.RowCount - 1 = 0 Then
                Me.TextBoxLoc.Text = ""
                Me.TextBoxNote.Text = ""
                Me.TextBoxSup.Text = ""
            Else
                DataGridViewFoncSite.Rows(0).Selected = True
                Me.TextBoxLoc.Text = DataGridViewFoncSite.SelectedCells(3).Value
                Me.TextBoxNote.Text = DataGridViewFoncSite.SelectedCells(5).Value
                Me.TextBoxSup.Text = DataGridViewFoncSite.SelectedCells(4).Value
                Me.oidValue.Text = DataGridViewFoncSite.SelectedCells(6).Value
            End If

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub DataGridViewFoncSite_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewFoncSite.CurrentCellChanged
        If Entrer = "debut" Then
            selection()
        End If
    End Sub
    Private Sub DataGridViewFoncSite_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewFoncSite.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            selection()
        End If
    End Sub


    Private Sub selection()
        Try
            If Not DataGridViewFoncSite.SelectedCells(0).ToString = "" Then
                Me.TextBoxLoc.Text = DataGridViewFoncSite.SelectedCells(3).Value
                Me.TextBoxNote.Text = DataGridViewFoncSite.SelectedCells(5).Value
                Me.TextBoxSup.Text = DataGridViewFoncSite.SelectedCells(4).Value
                Me.oidValue.Text = DataGridViewFoncSite.SelectedCells(6).Value
            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub


    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxLoc.Text = ""
        Me.TextBoxNote.Text = ""
        Me.TextBoxSup.Text = ""
    End Sub
    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Try
            Entrer = ""
            If action = "ajouter" Then
                db.myReq = "INSERT INTO  [dbo].[SITESTFONCIER]([Id_TypFonc],[ID_Sit],[LOCFON_FONC],[SRFFON_FONC],[NOTES_FONC],[SIGID]) VALUES('" & Me.ComboBoxCodeCatg.Text & "','" & db.idSite & "','" & Me.TextBoxLoc.Text & "','" & Me.TextBoxSup.Text & "','" & Me.TextBoxNote.Text & "','" & Me.oidValue.Text & "')"
                MsgBox(db.myReq)
            Else
                db.myReq = "UPDATE  [dbo].[SITESTFONCIER] SET [Id_TypFonc] = '" & Me.ComboBoxCodeCatg.Text & "',[ID_Sit] = '" & db.idSite & "',[LOCFON_FONC] = '" & Me.TextBoxLoc.Text & "',[SRFFON_FONC] = '" & Me.TextBoxSup.Text & "',[NOTES_FONC] = '" & Me.TextBoxNote.Text & "',[SIGID]='" & Me.oidValue.Text & "' WHERE [Id_Fonc]='" & Me.DataGridViewFoncSite.SelectedCells(0).Value & "')"
            End If
            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        majour()
    End Sub

    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Try
            db.myReq = " DELETE FROM  [dbo].[SITESTFONCIER]  WHERE [Id_Fonc]='" & Me.DataGridViewFoncSite.SelectedCells(0).Value & "')"
            db.Connecter(databaseName)
            db.Disconnecter()
            majour()
            If DataGridViewFoncSite.RowCount - 1 = 0 Then
                Return
            End If
            Dim listOid As List(Of String) = New List(Of String)

            listOid.Add(DataGridViewFoncSite.SelectedCells(6).Value)

            _workspace.deleteNodes(_layerName, listOid)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim typ1 As New TypologieNiv1Form(databaseName)
        typ1.Text = "Catégorie"
        typ1.Table = "TypFoncier"
        typ1.Visible = True
    End Sub

    Private Sub ComboBoxNomCatg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomCatg.Click
        majCatg()
    End Sub

    Private Sub ButtonMod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMod.Click
        action = "modifier"
    End Sub

    ''' <summary>
    ''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private _workspace As Object
    Private _layerName As String
    Private geoForm As GeoLocation = Nothing
    Property layerName() As String
        Get
            Return _layerName
        End Get
        Set(ByVal value As String)
            _layerName = value
        End Set
    End Property

    Property workspace() As Object
        Get
            Return _workspace
        End Get
        Set(ByVal value As Object)
            _workspace = value
        End Set
    End Property

    Private Sub btnGetGeo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetGeo.Click
        If geoForm IsNot Nothing AndAlso geoForm.Visible Then
            Return
        End If

        geoForm = New GeoLocation()
        geoForm.Show()
        geoForm.workspace = Me.workspace
        geoForm.oidParentText = oidValue

    End Sub
End Class